package org.compiere.report;

import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MBPartner;
import org.compiere.model.MOrg;
import org.compiere.model.X_A_Asset;
import org.compiere.process.ProcessInfo;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.Env;
import org.compiere.util.Msg;

import extend.org.compiere.utils.Util;


public class A_Asset_Form_OS1 extends SvrProcess {

	/** Current context */
	private Properties m_ctx;
	/**     */
	private ProcessInfo pi;

	int p_A_Asset_ID;
	
	String getSQlresult = null;
	String file;
	/**
	 * Prepare - e.g., get Parameters.
	 */
	@Override
	protected void prepare() {
		log.info("");
		m_ctx = Env.getCtx();
		pi = getProcessInfo();
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++) {
			String name = para[i].getParameterName();
			if (para[i].getParameter() == null)
				;
			else if (name.equals("A_Asset_ID")) 
				p_A_Asset_ID  = para[i].getParameterAsInt();
			else
				log.log(Level.SEVERE, "Unknown Parameter: " + name);
		}

	}
	
	@Override
	protected String doIt() throws Exception {
		file = Util.localFilePath + "A_Asset_Form_OS1_tmp.xls";
		MAttachmentEntry entry = Util.getAttachment(pi, m_ctx, file);
		if (entry == null)
			throw new AdempiereException(Msg.translate(m_ctx,"NotFoundTemplate"));

		Workbook wb = new HSSFWorkbook(entry.getInputStream());
		Sheet sheet = wb.getSheetAt(0);

		Row R = null;
		Cell C = null;
		
		Timestamp currdate = new Timestamp(System.currentTimeMillis());
		X_A_Asset asset = new X_A_Asset (getCtx(), p_A_Asset_ID, get_TrxName());
		
		MOrg org = MOrg.get(m_ctx,asset.getAD_Org_ID());
		MBPartner bpartner = MBPartner.get(m_ctx, org.getLinkedC_BPartner_ID(get_TrxName()));
		
		R = sheet.getRow(10);
		C = R.getCell(29);
		C.setCellValue(org.getName());
		
		C = R.getCell(60);
		C.setCellValue(bpartner.get_Value("FI_TIN").toString());
		R = sheet.getRow(11);
		C = R.getCell(33);
		C.setCellValue(asset.getName());
		R = sheet.getRow(26);
		C = R.getCell(0);
		C.setCellValue(new SimpleDateFormat("dd.MM.yyyy").format(currdate));
		if (asset.getC_BPartnerSR_ID()>0)
		{
			MBPartner bp = MBPartner.get(m_ctx, asset.getC_BPartnerSR_ID());
			R = sheet.getRow(26);
			C = R.getCell(61);
			C.setCellValue(bp.getName());			
		}
		
		BigDecimal bCost = asset.getAssetCost(currdate);
		
		R = sheet.getRow(31);
		if (bCost!=null) 
		{
			C = R.getCell(0);
			C.setCellValue(bCost.toString());
		}
		C = R.getCell(26);
		C.setCellValue(asset.getValue());
		C = R.getCell(33);
		C.setCellValue(asset.getSerNo());
		C = R.getCell(40);
		C.setCellValue("241000");
		C = R.getCell(48);
		C.setCellValue("242000");
		C = R.getCell(64);
		C.setCellValue(new SimpleDateFormat("dd.MM.yyyy").format(asset.getAssetServiceDate()));
		
		R = sheet.getRow(32);
		C = R.getCell(51);
		C.setCellValue(asset.getName());
		
		int row = 44;
		/*for(int i=0; i < count; i++, row++){
		if(i > 0)
			sheet.insertRow(row);*/
	//	line li = l.get(i);
//	// output datas ----------------------------------{		
		R = sheet.createRow(row);
		C = R.createCell(0);
		C.setCellValue(Integer.valueOf(row-43).toString());
		
		/*label = new Label(1, row, li.v2, valuestyle);
		sheet.mergeCells(1, row, 4, row);
		sheet.addCell(label);
		label = new Label(5, row, li.v3, valuestyle);
		sheet.mergeCells(5, row, 6, row);
		sheet.addCell(label);
		label = new Label(7, row, li.v4, valuestyle);
		sheet.mergeCells(7, row, 8, row);
		sheet.addCell(label);			
		label = new Label(9, row, li.v5, valuestyle);
		sheet.mergeCells(9, row, 10, row);
		sheet.addCell(label);			
		label = new Label(11, row, li.v6.toString().replace('.', ','), valuestyle);
		sheet.mergeCells(11, row, 12, row);
		sheet.addCell(label);*/

//		label = new Label(13, row, (new BigDecimal(0)).toString().replace('.', ','), valuestyle);
		/*number = new Number(13, row, new BigDecimal(0).doubleValue(), vsNumber);
		sheet.addCell(number);

		number = new Number(14, row, new BigDecimal(0).doubleValue(), vsNumber);
		sheet.addCell(number);*/

		
		/*BigDecimal v16 = li.v9;
		number = new Number(15, row, v16.doubleValue(), vsNumber);
		sheet.addCell(number);
		
		BigDecimal v17 = v16.multiply(li.v6);
		number = new Number(16, row, v17.doubleValue(), vsNumber);
		sheet.addCell(number);*/
	/*}*/
		
		row += 18;
		/*if(chiefComm!=null){
		m = getMember(chiefComm);
		label = new Label(3, row, m.position, vsMember);
		sheet.mergeCells(3, row, 7, row);
		sheet.addCell(label);
		label = new Label(14, row, m.fio, vsMember);
		sheet.mergeCells(14, row, 16, row);
		sheet.addCell(label);
	}*/		
		
		
		// Write the output to a file
		FileOutputStream fileOut = new FileOutputStream(file);
		wb.write(fileOut);
		fileOut.close();
		try {
				//Forces Excel to use the specified file as a template for the default workbook. 
				Runtime.getRuntime().exec("cmd /c start excel " + file+" /t");
			} 
		catch (Exception e) 
			{
				e.printStackTrace();
			}

		return Msg.translate(m_ctx, "Success");
	}

}